package next.SupportFunctionality;


import java.io.IOException;
import next.TemplateActivity.R;
import next.control.ApplicationController;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class VideoMediaPlayer extends Activity implements SurfaceHolder.Callback {

	//--Variables--
	private MediaRecorder mediaRecorder;
	private MediaPlayer mediaPlayer=new MediaPlayer();
	private SurfaceView surface;
	private SurfaceHolder holder;
	private String defaultURL="";

	/**Called when the video Media Player is started*/
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.support_video_media_player_layout);

		surface = (SurfaceView) findViewById(R.id.surface);
		holder = surface.getHolder();
		holder.addCallback(this);
		holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		holder.setFixedSize(400, 300);
		
			Bundle b = getIntent().getExtras();
			if (b!=null)
			{	
				defaultURL = b.getString("URL");
			}
		}

	/**Creates media player and assembling this to a surface is possible to play the URL if exist*/
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		
		//File sdCard = Environment.getExternalStorageDirectory();
		
		try {
				mediaPlayer.setDisplay(holder);
				//mediaPlayer.setDataSource(sdCard.getAbsolutePath()+"/BM_LazySong.3gp");
				Toast.makeText(getBaseContext(), defaultURL, Toast.LENGTH_LONG).show();
				mediaPlayer.setDataSource(defaultURL);
				mediaPlayer.prepare();
				mediaPlayer.start();
				
				/*//--To jump to a specific section use this.
				int pos = mediaPlayer.getCurrentPosition();
				int duration = mediaPlayer.getDuration();
				mediaPlayer.seekTo(pos + (duration-pos)/10);
				//--[ . . . wait for a duration . . . ]
				//mediaPlayer.stop();*/
				
				if (!mediaPlayer.isLooping())
					mediaPlayer.setLooping(true);
				//--Not necessary to use permissions
				mediaPlayer.setScreenOnWhilePlaying(true);
				//--Right and left channels
				mediaPlayer.setVolume(1f, 0.5f);
						
				/*if (mediaRecorder == null) {
						//try {
							mediaRecorder=new MediaRecorder();
							mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
							mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
			
							mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
			
							mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
							mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
							mediaRecorder.setOutputFile(sdCard.getAbsolutePath()+"/DCIM/Camera/myoutputkique.mp4");
			
							mediaRecorder.setPreviewDisplay(holder.getSurface());
							mediaRecorder.prepare();
							mediaRecorder.start();
					}*/
			} catch (IllegalArgumentException e) {
				Log.d("MEDIA_PLAYER IA", e.getMessage());
			} catch (IllegalStateException e) {
				Log.d("MEDIA_PLAYER ISE", e.getMessage());
			} catch (IOException e) {
				Log.d("MEDIA_PLAYER IO", e.getMessage());
			
		}
	}

	/**MediaPlayer is releasing resources */
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		mediaPlayer.stop();  
		mediaPlayer.release();
		/*mediaRecorder.stop();
		mediaRecorder.release();*/
	}

	/**Necessary Method: Not Implemented */
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {}
	
	 
}